package seelction;

/**
 * Created by mHandler on 2018/5/6 0006.
 * Description:algo select sort 基于对象的选择排序
 *
 */
public class ObjectSelectSort {

    private ObjectSelectSort() {}

    public static void sort(Comparable[] arr){
        int n = arr.length;
        for (int i = 0;i<n; i++){
            int minIndex = i;
            for (int j=i+1;j<n;j++){
                if (arr[minIndex].compareTo(arr[j])>0){
                    minIndex=j;
                }
            }
            swap(arr,i,minIndex);
        }
    }
    public static void swap(Object[] arr ,int i,int minIndex){
        Object t=arr[minIndex];
        arr[minIndex]=arr[i];
        arr[i]=t;
    }

    public static void main(String[] args) {
        Integer[] arr = {10, 9, 8, 7, 6, 5, 4, 3, 22, 2, 1};
        sort(arr);
        for (int i = 0; i < arr.length; i++)
            System.out.print(arr[i] + " ");

        System.out.println("");
        // 测试Double
        Double[] b = {4.4, 3.3, 2.2, 1.1};
        sort(b);
        for( int i = 0 ; i < b.length ; i ++ )
            System.out.print(b[i]+" ");

        System.out.println("");

        // 测试String
        String[] c = {"D", "C", "B", "A"};
        sort(c);
        for( int i = 0 ; i < c.length ; i ++ )
            System.out.print(c[i]+" ");


        System.out.println("");

        // 测试自定义的类 Student
        Student[] d = new Student[4];
        d[0] = new Student("D",90);
        d[1] = new Student("C",100);
        d[2] = new Student("B",95);
        d[3] = new Student("A",95);
        sort(d);
        for( int i = 0 ; i < d.length ; i ++ )
            System.out.println(d[i]);
    }
}
